import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'

const useRegStore = defineStore('regStore', () => {
	// 手机号码正则匹配
	const regPhone = /^1[3456789]\d{9}$/
	// 判断一个数为整数的正则匹配
	const regIntger = /^-?\d+$/

	// 邮箱验证
	const emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/

	const isInteger = (val) => {
		return regIntger.test(val)
	}

	const handleCheckEmail = (val) => {
		return emailReg.test(val)
	}

	// 验证手机号
	const phoneIsOk = (phone) => regPhone.test(phone)

	// 验证搜索条件--学号/职工号
	const handleCheckId = (val) => {
		if (!val) {
			ElMessage.warning({ message: '请输入搜索内容', duration: 1000 })
			return false
		}
		if (!Number.isInteger(val)) {
			ElMessage.warning({ message: '输入非法：学号/职工号必须为数字', duration: 1000 })
			return false
		}
		return true
	}
	const handleCheckDorNumber = (roomId) => {
		if (!Number.isInteger(roomId)) {
			ElMessage.warning({ message: '房间号非法，必须为数字', duration: 1000 })
			return false
		}
		if (roomId < 101 || roomId > 580) {
			ElMessage.warning({ message: '房间号可选范围：101-580 ', duration: 1000 })
			return false
		}
		return true
	}

	return { regPhone, isInteger, phoneIsOk, handleCheckId, handleCheckDorNumber, handleCheckEmail }
})

export default useRegStore
